package com.xu.cloud.log.listener;

import com.xu.cloud.log.XuLog4j2LoggingSystem;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.boot.context.logging.LoggingApplicationListener;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;

/**
 * log4j2环境变量设置监听器
 *
 * @author xuguan
 * @since 2024/11/1
 */
public class Log4j2SystemPropertiesListener implements ApplicationListener<ApplicationStartingEvent>, Ordered {

    @Override
    public void onApplicationEvent(ApplicationStartingEvent event) {
        openAnsi();
        setLoggingSystem();
    }

    private void openAnsi() {
        // 开启ANSI彩色日志
        System.setProperty("log4j.skipJansi", "false");
    }

    private void setLoggingSystem() {
        // 配置日志系统
        System.setProperty(LoggingSystem.SYSTEM_PROPERTY, XuLog4j2LoggingSystem.class.getName());
    }

    @Override
    public int getOrder() {
        return LoggingApplicationListener.DEFAULT_ORDER - 1;
    }
}
